package com.cxrj.zhsq.workorderManage.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.personnelManage.dao.PropertyPersonnelMapper;
import com.cxrj.zhsq.workorderManage.dao.PropertyWorkorderMapper;
import com.cxrj.zhsq.workorderManage.entity.PropertyWorkorder;
import com.cxrj.zhsq.workorderManage.service.PropertyWorkorderService;

@Service
public class PropertyWorkorderServiceImpl implements PropertyWorkorderService {

	@Resource
	private PropertyWorkorderMapper propertyWorkorderMapper;

	@Resource
	private PropertyPersonnelMapper propertyPersonnelMapper;

	@Override
	public Map<String, Object> seletePropertyWorkorderPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		sqlMap.put("title", request.getParameter("title"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyWorkorderMapper.selectByCount(sqlMap) + "";
		List<PropertyWorkorder> list = propertyWorkorderMapper.selectPropertyWorkordersPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> insertPropertyWorkorder(PropertyWorkorder propertyWorkorder) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyWorkorder.getId().equals("")) {
			propertyWorkorder.setId(UUID.randomUUID().toString());
			propertyWorkorder.setState("0");
			propertyWorkorder.setCreateTime(new Date());
			propertyWorkorder.setS2("1");
			c = propertyWorkorderMapper.insertSelective(propertyWorkorder);
		} else {
			c = propertyWorkorderMapper.updateByPrimaryKeySelective(propertyWorkorder);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updatePropertyWorkorder(PropertyWorkorder propertyWorkorder) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyWorkorderMapper.updateByPrimaryKeySelective(propertyWorkorder);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyWorkorderById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyWorkorder propertyWorkorder = propertyWorkorderMapper.selectByPrimaryKey(id);
		if (propertyWorkorder.getpId() != null && !propertyWorkorder.getpId().equals("")) {
			propertyWorkorder.setList(propertyPersonnelMapper.selectByIds(Arrays.asList(propertyWorkorder.getpId().split(","))));
		}
		resultMap.put("workorder", propertyWorkorder);
		return resultMap;
	}
}
